package com.youyi.yyfilepickerlibrary.core.activity;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lkx.liandianqinew.R;
import com.youyi.yyfilepickerlibrary.core.BaseFileFragment;
import com.youyi.yyfilepickerlibrary.core.SelectOptions;
import com.youyi.yyfilepickerlibrary.core.adapter.FileListAdapter;
import com.youyi.yyfilepickerlibrary.core.loader.EssMimeTypeCollection;
import com.youyi.yyfilepickerlibrary.core.model.EssFile;
import com.youyi.yyfilepickerlibrary.core.model.FileScanActEvent;
import com.youyi.yyfilepickerlibrary.core.model.FileScanFragEvent;
import com.youyi.yyfilepickerlibrary.core.model.FileScanSortChangedEvent;
import java.util.ArrayList;
import java.util.List;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class FileTypeListFragment extends BaseFileFragment implements BaseQuickAdapter.OnItemClickListener, EssMimeTypeCollection.EssMimeTypeCallbacks {
    private static final String ARG_FileType = "ARG_FileType";
    private static final String ARG_IsSingle = "mIsSingle";
    private static final String ARG_Loader_Id = "ARG_Loader_Id";
    private static final String ARG_MaxCount = "mMaxCount";
    private static final String ARG_SortType = "mSortType";
    private FileListAdapter mAdapter;
    private String mFileType;
    private boolean mIsSingle;
    private int mLoaderId;
    private int mMaxCount;
    private EssMimeTypeCollection mMimeTypeCollection = new EssMimeTypeCollection();
    private RecyclerView mRecyclerView;
    private List<EssFile> mSelectedFileList = new ArrayList();
    private int mSortType;
    private boolean mSortTypeHasChanged = false;

    @Override // com.youyi.yyfilepickerlibrary.core.loader.EssMimeTypeCollection.EssMimeTypeCallbacks
    public void onFileReset() {
    }

    public static FileTypeListFragment newInstance(String str, boolean z, int i, int i2, int i3) {
        FileTypeListFragment fileTypeListFragment = new FileTypeListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ARG_FileType, str);
        bundle.putBoolean(ARG_IsSingle, z);
        bundle.putInt(ARG_MaxCount, i);
        bundle.putInt(ARG_SortType, i2);
        bundle.putInt(ARG_Loader_Id, i3);
        fileTypeListFragment.setArguments(bundle);
        return fileTypeListFragment;
    }

    @Override // android.support.v4.app.Fragment
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (getArguments() != null) {
            this.mFileType = getArguments().getString(ARG_FileType);
            this.mIsSingle = getArguments().getBoolean(ARG_IsSingle);
            this.mMaxCount = getArguments().getInt(ARG_MaxCount);
            this.mSortType = getArguments().getInt(ARG_SortType);
            this.mLoaderId = getArguments().getInt(ARG_Loader_Id);
        }
    }

    @Override // com.youyi.yyfilepickerlibrary.core.BaseFileFragment
    public int getFragmentLayout() {
        return R.layout.fragment_file_type_list;
    }

    /* access modifiers changed from: protected */
    @Override // com.youyi.yyfilepickerlibrary.core.BaseFileFragment
    public void lazyLoad() {
        this.mMimeTypeCollection.load(this.mFileType, this.mSortType, this.mLoaderId);
    }

    /* access modifiers changed from: protected */
    @Override // com.youyi.yyfilepickerlibrary.core.BaseFileFragment
    public void initUI(View view) {
        this.mMimeTypeCollection.onCreate(getActivity(), this);
        EventBus.getDefault().register(this);
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rcv_file_list_scan);
        this.mRecyclerView = recyclerView;
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        FileListAdapter fileListAdapter = new FileListAdapter(new ArrayList());
        this.mAdapter = fileListAdapter;
        this.mRecyclerView.setAdapter(fileListAdapter);
        this.mAdapter.bindToRecyclerView(this.mRecyclerView);
        this.mAdapter.setEmptyView(R.layout.loading_layout);
        this.mAdapter.setOnItemClickListener(this);
        super.initUI(view);
    }

    @Subscribe
    public void onFreshCount(FileScanActEvent fileScanActEvent) {
        this.mMaxCount = fileScanActEvent.getCanSelectMaxCount();
    }

    @Subscribe
    public void onFreshSortType(FileScanSortChangedEvent fileScanSortChangedEvent) {
        this.mSortType = fileScanSortChangedEvent.getSortType();
        if (this.mLoaderId == fileScanSortChangedEvent.getCurrentItem() + 3) {
            this.mMimeTypeCollection.load(this.mFileType, this.mSortType, this.mLoaderId);
        } else {
            this.mSortTypeHasChanged = true;
        }
    }

    @Override // com.youyi.yyfilepickerlibrary.core.BaseFileFragment, android.support.v4.app.Fragment
    public void setUserVisibleHint(boolean z) {
        super.setUserVisibleHint(z);
        if (getUserVisibleHint() && !this.isFirstLoad && this.mSortTypeHasChanged) {
            this.mSortTypeHasChanged = false;
            this.mAdapter.setNewData(new ArrayList());
            this.mAdapter.setEmptyView(R.layout.loading_layout);
            this.mMimeTypeCollection.load(this.mFileType, this.mSortType, this.mLoaderId);
        }
    }

    @Override // com.chad.library.adapter.base.BaseQuickAdapter.OnItemClickListener
    public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
        EssFile essFile = (EssFile) this.mAdapter.getData().get(i);
        if (this.mIsSingle) {
            this.mSelectedFileList.add(essFile);
            EventBus.getDefault().post(new FileScanFragEvent(essFile, true));
        } else if (((EssFile) this.mAdapter.getData().get(i)).isChecked()) {
            int findFileIndex = findFileIndex(essFile);
            if (findFileIndex != -1) {
                this.mSelectedFileList.remove(findFileIndex);
                EventBus.getDefault().post(new FileScanFragEvent(essFile, false));
                ((EssFile) this.mAdapter.getData().get(i)).setChecked(!((EssFile) this.mAdapter.getData().get(i)).isChecked());
                this.mAdapter.notifyItemChanged(i, "");
            }
        } else if (this.mMaxCount <= 0) {
            RecyclerView recyclerView = this.mRecyclerView;
            Snackbar.make(recyclerView, "您最多只能选择" + SelectOptions.getInstance().maxCount + "个。", -1).show();
        } else {
            this.mSelectedFileList.add(essFile);
            EventBus.getDefault().post(new FileScanFragEvent(essFile, true));
            ((EssFile) this.mAdapter.getData().get(i)).setChecked(!((EssFile) this.mAdapter.getData().get(i)).isChecked());
            this.mAdapter.notifyItemChanged(i, "");
        }
    }

    private int findFileIndex(EssFile essFile) {
        for (int i = 0; i < this.mSelectedFileList.size(); i++) {
            if (this.mSelectedFileList.get(i).getAbsolutePath().equals(essFile.getAbsolutePath())) {
                return i;
            }
        }
        return -1;
    }

    @Override // android.support.v4.app.Fragment
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        this.mMimeTypeCollection.onDestroy();
    }

    @Override // com.youyi.yyfilepickerlibrary.core.loader.EssMimeTypeCollection.EssMimeTypeCallbacks
    public void onFileLoad(List<EssFile> list) {
        Log.i("TAG", "size --> " + list.size());
        this.mAdapter.setNewData(list);
        this.mRecyclerView.scrollToPosition(0);
        if (list.isEmpty()) {
            this.mAdapter.setEmptyView(R.layout.empty_file_list);
        }
    }
}
